package app.server.project.main.service.service;

import app.server.project.core.constants.Def;
import app.server.project.core.constants.TcpCmd;
import app.server.project.core.websocket.WSMgr;
import app.server.project.core.websocket.model.Online;
import app.server.project.main.service.bo.event.CommonRankEvent;
import app.server.project.main.service.bo.event.DayRankInnerEvent;
import app.server.project.main.service.bo.event.WeekRankInnerEvent;
import app.server.project.main.service.constants.GlobalCfgKey;
import app.server.project.main.service.db.dao.cfg.GlobalCfgDao;
import app.server.project.main.service.db.dao.cfg.RaceCfgDao;
import app.server.project.main.service.db.dao.user.UserExtLock;
import app.server.project.main.service.db.dao.user.UserInfoDao;
import app.server.project.main.service.db.entity.user.UserInfoEntity;
import app.server.project.main.service.dto.cms.LockAccountReq;
import app.server.project.main.service.dto.cms.UserInfoResp;
import app.server.project.main.service.service.lv.UserLvService;
import app.server.project.main.service.service.rank.RankType;
import lombok.AllArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class UserInfoService {


    private UserInfoDao userInfoDao;
    private UserLvService userLvService;
    private EnergyService energyService;
    private ResService resService;
    private GlobalCfgDao globalCfgDao;
    private RaceCfgDao raceCfgDao;
    private ApplicationEventPublisher applicationEventPublisher;
    private UserExtLock userExtLock;


    public UserInfoResp getBaseInfo(long userId) {
        UserInfoEntity userInfoEntity = this.userInfoDao.getUserInfoById(userId);
        //等级信息
        var lvDto = this.userLvService.getDto(userId);
        //体力信息
        var energy = this.energyService.getDto(userId);
        var aiDto = this.resService.getAiCoinDto(userId);
        var ret = UserInfoResp.of(userInfoEntity, energy, lvDto, aiDto);
        var ext = this.userExtLock.getUserExtByUserExtId(userId);
        ret.setUnlockRaceAll(ext.getUnlockRaceAll());
        ret.setPropClickNum(ext.getPropClickNum());
        ret.setChanceTime(ext.getChanceTimeForSeconds());
        ret.setVersion(this.globalCfgDao.getStr(GlobalCfgKey.CLIENT_VERSION));
        return ret;
    }

    public UserInfoResp getInviteInfo(long userId, int btcType, int vipType) {
        UserInfoEntity userInfoEntity = this.userInfoDao.getUserInfoById(userId);
        var ret = UserInfoResp.ofInvite(userInfoEntity, btcType, vipType);
        return ret;
    }

    //@EventListener(Online.class)
    public void pushUserInfo(Online online) {
        var dto = this.getBaseInfo(online.getUserId());
        WSMgr.push(online.getUserId(), TcpCmd.UserInfo, dto);
    }


    public void setInner(LockAccountReq lockAccountReq) {
        var db = this.userExtLock.getUserExtByUserExtId(lockAccountReq.getUserId());
        db.setInnerType(Def.True);
        this.raceCfgDao.getAll().forEach(v -> {
            this.applicationEventPublisher.publishEvent(WeekRankInnerEvent.of(v.getId(), lockAccountReq.getUserId(), Def.Zero));
            this.applicationEventPublisher.publishEvent(DayRankInnerEvent.of(v.getId(), lockAccountReq.getUserId(), Def.Zero));
        });
        this.applicationEventPublisher.publishEvent(CommonRankEvent.of(RankType.InviteDay, lockAccountReq.getUserId(), Long.MAX_VALUE, false));
        this.applicationEventPublisher.publishEvent(CommonRankEvent.of(RankType.InviteSum, lockAccountReq.getUserId(), Long.MAX_VALUE, false));
        this.applicationEventPublisher.publishEvent(CommonRankEvent.of(RankType.InviteWeek, lockAccountReq.getUserId(), Long.MAX_VALUE, false));
    }

    public void canRank(LockAccountReq lockAccountReq) {
        var db = this.userExtLock.getUserExtByUserExtId(lockAccountReq.getUserId());
        db.setInnerType(Def.FALSE);
    }
}
